package com.hqyj.springboot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.springboot.modules.account.dao.ResourceDao;
import com.hqyj.springboot.modules.account.dao.ResourceDao2;
import com.hqyj.springboot.modules.account.dao.RoleResourceDao;
import com.hqyj.springboot.modules.account.dao.RoleResourceDao2;
import com.hqyj.springboot.modules.account.entity.Resource;
import com.hqyj.springboot.modules.account.entity.RoleResource;
import com.hqyj.springboot.modules.account.service.ResourceService;
import com.hqyj.springboot.modules.common.vo.Result;
import com.hqyj.springboot.modules.common.vo.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private ResourceDao2 resourceDao2;
     @Autowired
     private RoleResourceDao roleResourceDao;
    @Autowired
     private RoleResourceDao2 roleResourceDao2;


    @Override
    @Transactional
    public Result<Resource> addResource(Resource resource) {
        resource.setCreateDate(LocalDateTime.now());
        resource.setUpdateDate(LocalDateTime.now());
        resourceDao.saveAndFlush(resource);
        if (resource.getRoles() != null){
            resource.getRoles().stream().forEach(item -> {
                RoleResource roleResource = new RoleResource();
                roleResource.setCreateDate(LocalDateTime.now());
                roleResource.setUpdateDate(LocalDateTime.now());
                roleResource.setRoleId(item.getId());
                roleResource.setResourceId(resource.getId());
                roleResourceDao2.addRoleResource(roleResource);
            });
        }
        return new Result<Resource>(Result.ResultStatus.SUCCESS.code, "资源添加成功!",resource);
    }

    @Override
    @Transactional
    public Result<Resource> updateResource(Resource resource) {
        resource.setUpdateDate(LocalDateTime.now());
        resourceDao2.updateResource(resource);
        roleResourceDao2.deleteRoleResourceByResourceId(resource.getId());
        if (resource.getRoles() != null){
            resource.getRoles().stream().forEach(item ->{
                RoleResource roleResource = new RoleResource();
                roleResource.setCreateDate(LocalDateTime.now());
                roleResource.setUpdateDate(LocalDateTime.now());
                roleResource.setRoleId(item.getId());
                roleResource.setResourceId(resource.getId());
                roleResourceDao2.addRoleResource(roleResource);
            });
        }
        return new Result<Resource>(Result.ResultStatus.SUCCESS.code, "资源修改成功!",resource);
    }

    @Override
    @Transactional
    public Result<Object> deleteResourceById(int id) {
        resourceDao2.deleteResourceById(id);
        roleResourceDao2.deleteRoleResourceByResourceId(id);

        return new Result<Object>(Result.ResultStatus.SUCCESS.code, "删除成功!");
    }

    @Override
    public Resource getResourceById(int id) {
        return resourceDao2.getResourceById(id);
    }

    @Override
    public List<Resource> getResourcesByRoleId(int roleId) {
        return resourceDao2.getResourcesByRoleId(roleId);
    }

    @Override
    public PageInfo<Resource> getResourcesBySearch(Search search) {
       search.init();
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());

        return new PageInfo<Resource>(Optional.ofNullable(resourceDao2.getResourcesBySearch(search)).orElse(Collections.emptyList()));
    }
}
